/**
 * 
 */
package com.google.code.pos;

import com.google.code.pos.domain.Item;
import com.google.code.pos.service.JdbcService;
import com.google.code.pos.service.impl.JdbcServiceImpl;
import com.google.code.pos.ui.MainFrame;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.awt.EventQueue;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * @author jimmy
 *
 */
public class Main {

    private static Properties properties = new Properties();
    private static MainFrame mainFrame;

    /**
     * the function of this method is load file properties,
     * and getting the property from file properties for database source.
     */
    private static DataSource setDbSource() {
        try {
            properties.load(Main.class.getResourceAsStream("/com/google/code/pos/mysql.properties"));
            if (!properties.isEmpty()) {
                System.out.println("file properties found.");
                //setting mysql data source for connection
                MysqlDataSource mysqlSource = new MysqlDataSource();
                mysqlSource.setUser(properties.getProperty("mysql.user"));
                mysqlSource.setPassword(properties.getProperty("mysql.password"));
                mysqlSource.setServerName(properties.getProperty("mysql.url"));
                mysqlSource.setPortNumber(Integer.parseInt(properties.getProperty("mysql.port")));
                mysqlSource.setDatabaseName(properties.getProperty("mysql.db"));
                return mysqlSource;
            }
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void main(String args[]) {
//        EventQueue.invokeLater(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    if (System.getProperty("os.name").contains("Linux")) {
//                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
//                    } else if (System.getProperty("os.name").contains("Windows")) {
//                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//                    }
//                } catch (ClassNotFoundException ex) {
//                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (InstantiationException ex) {
//                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (IllegalAccessException ex) {
//                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (UnsupportedLookAndFeelException ex) {
//                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                mainFrame = new MainFrame();
//                mainFrame.showFrame();
//            }
//        });

        JdbcService service = new JdbcServiceImpl();
        try {
            System.out.println("db source : " + setDbSource().getConnection().getCatalog());
        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        service.setConnection(setDbSource());

        List<Item> listItem = service.findAllItem();
        for (Item item : listItem) {
            System.out.println("id = " + item.getId() + ", name = " + item.getName());
        }
        
    }
}
